package com.mf.morefun.ui.moreread;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.mf.morefun.adapter.ReadAdapter;
import com.mf.morefun.adapter.ReadTypeAdapter;
import com.mf.morefun.base.BaseBusActivity;
import com.mf.morefun.bean.ReadBean;
import com.mf.morefun.bean.ReadTypeBean;
import com.mf.morefun.bean.ResultBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.requestbean.RequesBannerBean;
import com.mf.morefun.requestbean.RequestReadBean;
import com.mf.morefun.ui.dohomework.HomeworkListActivity;
import com.mf.morefun.ui.personnelmanagement.PersonnelManagementActivity;
import com.mf.morefun.ui.userinfo.LoginActivity;
import com.mf.morefun.util.GetUserInfo;
import com.mf.morefun.util.IsLoginBean;
import com.mf.morefun.widget.NetErrorPop;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoreReadingActivity extends BaseBusActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.lv_type)
    ListView lvType;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.gv_info)
    GridView gvInfo;
    private ReadTypeAdapter adapter;
    private ReadAdapter mReadAdapter;
    private List<ReadTypeBean> mReadTypeBeanList = new ArrayList<>();
    private List<ReadBean> mReadList = new ArrayList<>();
    private SPUtils mSputils;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_moreread;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSputils = new SPUtils(MoreReadingActivity.this);
        if (!IsLoginBean.isNo(false)){
            GetUserInfo.initLogin(mSputils.getString(Configuration.USERPHONE), mSputils.getString(Configuration.USERPASSWORD));
        }
        adapter = new ReadTypeAdapter(MoreReadingActivity.this, mReadTypeBeanList);
        adapter.setSelectItem(0);
        mReadAdapter = new ReadAdapter(MoreReadingActivity.this, mReadList);
        gvInfo.setAdapter(mReadAdapter);
        lvType.setAdapter(adapter);
        //查询
        if (null != MoreFunApplication.loginBean.getResult().getOrgan()){
            initTypeList();
        }
        lvType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != adapter){
                    tvTitle.setText(mReadTypeBeanList.get(position).getClassification().getName());
                    adapter.setSelectItem(position);
                    adapter.notifyDataSetChanged();
                    initRead(mReadTypeBeanList.get(position).getClassification().getId());
                }
            }
        });
        gvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent mIntent = new Intent(MoreReadingActivity.this, MoreReadDetailActivity.class);
                mIntent.putExtra("READID", mReadList.get(position).getId());
                mIntent.putExtra("READNAME", mReadList.get(position).getName());
                mIntent.putExtra("type", getIntent().getStringExtra("type"));
                startActivity(mIntent);
            }
        });
    }

    private void initTypeList() {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.READTYPELIST);
        RequesBannerBean bannerBean = new RequesBannerBean();
        RequesBannerBean.OrganBean organBean = new RequesBannerBean.OrganBean();
        organBean.setId(MoreFunApplication.loginBean.getResult().getOrgan().getId());
        bannerBean.setOrgan(organBean);
        params.setBodyContent(JSON.toJSONString(bannerBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mReadTypeBeanList.clear();
                if (!StringUtils.isEmpty(result)){
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0){
                        if (!StringUtils.isEmpty(resultBean.getResult())){
                            List<ReadTypeBean> readTypeBeanList = JSON.parseArray(resultBean.getResult(), ReadTypeBean.class);
                            mReadTypeBeanList.addAll(readTypeBeanList);
                            tvTitle.setText(mReadTypeBeanList.get(0).getClassification().getId());
                            //根据id查询内容
                            initRead(readTypeBeanList.get(0).getClassification().getId());
                        }else {
                            ToastUtils.show(getApplicationContext(), resultBean.getMessage(), 100);
                        }
                    }else {
                        ToastUtils.show(getApplicationContext(), resultBean.getMessage(), 100);
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initRead(String id) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.READLIST);
        RequestReadBean.ClassificationBean classificationBean = new RequestReadBean.ClassificationBean();
        classificationBean.setId(id);
        RequestReadBean requestReadBean = new RequestReadBean();
        requestReadBean.setClassification(classificationBean);
        params.setBodyContent(JSON.toJSONString(requestReadBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mReadList.clear();
                if (!StringUtils.isEmpty(result)){
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0){
                        if (!StringUtils.isEmpty(resultBean.getResult())){
                            List<ReadBean> readBeanList = JSON.parseArray(resultBean.getResult(), ReadBean.class);
                            mReadList.addAll(readBeanList);
                        }else {
                            ToastUtils.show(getApplicationContext(), "数据为空", 100);
                        }
                    }else {
                        ToastUtils.show(getApplicationContext(), resultBean.getMessage(), 100);
                    }
                }
                mReadAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop(MoreReadingActivity.this);
                    errorPop.showPop("");
                    break;
            }
        }
    };

    @Override
    protected void initData() {

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
